<template>
  <div>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="userName">
        <el-input
          type="test"
          v-model="ruleForm.userName"
          autocomplete="off"
          style="width: 300px"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="userId">
        <el-input
          type="test"
          v-model="ruleForm.userId"
          autocomplete="off"
          style="width: 300px"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="userPwd">
        <el-input
          type="password"
          v-model="ruleForm.userPwd"
          style="width: 300px"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-upload
          action="http://localhost:88/api/pms/user/upload"
          class="upload-demo"
          :on-success="uploadFile"
          :on-remove="handleRemove"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item label="图片" prop="head">
        <el-input
          type="test"
          v-model="ruleForm.head"
          style="width: 300px"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button type="primary" @click="loginForm('ruleForm')"
          >登录</el-button
        >
        <el-button type="info" @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入手机号"));
      } else {
        callback();
      }
    };
    var validatePass3 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    var validatePass4 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请上传图片"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        userName: "",
        userId: "",
        userPwd: "",
        head: "",
      },
      rules: {
        userName: [{ validator: validatePass, trigger: "blur" }],
        userId: [{ validator: validatePass2, trigger: "blur" }],
        userPwd: [{ validator: validatePass3, trigger: "blur" }],
        head: [{ validator: validatePass4, trigger: "blur" }],
      },
      baseUrl: "http://localhost:88/api",
    };
  },
  methods: {
    loginForm(){
      this.$router.push("/");
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    uploadFile(file) {
      this.ruleForm.head = file.url;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
            .post(`${this.baseUrl}/pms/user/register`, this.ruleForm)
            .then(({ data }) => {
              console.log(data);
              if (data && data.code === 0) {
                this.$router.push("/");
              } else {
                this.dataList = [];
                this.totalPage = 0;
              }
              this.dataListLoading = false;
            });
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>